package org.xinjiang.tarim.travel.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xinjiang.tarim.travel.model.AttentionMdd;
import org.xinjiang.tarim.travel.service.AttentionMddService;
import org.xinjiang.tarim.travel.utils.Constants;
import org.xinjiang.tarim.travel.utils.JsonResult;
import org.xinjiang.tarim.travel.vo.Criteria;
import org.xinjiang.tarim.travel.vo.PageBean;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/attention")
public class AttentionMddApi {
    @Autowired
    AttentionMddService service;

    /**
     * 关注目的地
     * @param attentionMdd
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResult add(@RequestBody AttentionMdd attentionMdd){
        JsonResult jsonResult;
        try {
            Criteria criteria = new Criteria();
            criteria.addExtField("mddId", attentionMdd.getMddId());
            criteria.addExtField("openid", attentionMdd.getOpenid());
            List list = service.selectByConditionWithOutPage(criteria);
            if(list.size() == 0){
                int i = service.insert(attentionMdd);
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "已关注");
                return jsonResult;
            }
            jsonResult = new JsonResult(Constants.STATUS_FAIL, "已关注");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "关注异常");
        }
        return jsonResult;
    }

    /**
     * 取消关注
     * @param attentionMdd
     * @return
     */
    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public JsonResult delete(@RequestBody AttentionMdd attentionMdd){
        JsonResult jsonResult;
        try {
            int i = service.deleteByPrimaryKey(attentionMdd);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "取消关注");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

    /**
     * 条件查询（分页/不分页）
     * @param pageIndex
     * @param pageSize
     * @param mddId
     * @param openid
     * @return
     */
    @RequestMapping("/getByCondition")
    public JsonResult getByCondition(@RequestParam(value = "pageIndex", defaultValue = "") Integer pageIndex,
                                     @RequestParam(value = "pageSize", defaultValue = "") Integer pageSize,
                                     @RequestParam(value = "mddId", defaultValue = "") String mddId,
                                     @RequestParam(value = "openid", defaultValue = "") String openid){
        JsonResult jsonResult;
        try {
            if (openid == null || openid.equals("")) {
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "请先登录账号哟");
                return jsonResult;
            }

            PageBean pageBean = null;
            if(pageIndex != null && pageSize != null){
                pageBean = new PageBean(pageIndex, pageSize);
            }
            Criteria criteria = new Criteria();
            criteria.addExtField("mddId", mddId);
            criteria.addExtField("openid", openid);
            if(pageBean == null ){
                List list = service.selectByConditionWithOutPage(criteria);
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功", list);
                return jsonResult;
            }else{
                pageBean = service.selectByConditionWithPage(criteria, pageBean);
                jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "成功", pageBean);
                return jsonResult;
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return jsonResult;
    }

}
